package com.service.socket.process;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;

import com.service.cache.CacheManager;
import com.service.other.FileConser;
import com.service.other.Logger;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;

public class SendCommand {

	/**
	 * 发送
	 * @param bean
	 */
	public static void send(Packet packet) {
		try {
			Channel channel = CacheManager.instance().getChannel(packet.getDeviceNo());
			byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(packet.toString()));
			ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
			channel.write(buffer);
			FileConser.save(packet.getDeviceNo(),packet.getRawData(), "send");
			//System.out.println("server -> dtu : " + packet.toString());
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	public static void sendCtrlDtu(Packet packet){
		try {
			if(CacheManager.instance().ctrlDtuChannel.containsKey(packet.getDeviceNo())){
				Channel channel = CacheManager.instance().ctrlDtuChannel.get(packet.getDeviceNo());
				byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(packet.toString()));
				ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
				channel.write(buffer);
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	public static void sendCtrlUser(Packet packet){
		try {
			for(Channel channel :CacheManager.instance().ctrlUserChannel.values()){
				if(channel.isWritable()){
					System.out.println(packet.toString());
					byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(packet.toString()));
					ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
					channel.write(buffer);
				}
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	/**
	 * @param channel
	 * @param packet
	 * @param status
	 * @param dataArea
	 */
	public static void replyDtu(Channel channel,Packet packet) {
		try {
			byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(packet.toString()));
			ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
			channel.write(buffer);
			FileConser.save(packet.getDeviceNo(),packet.toString(), "reply");
			//System.out.println("server -> dtu : " + packet.toString());
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	/**
	 * @param channel
	 * @param packet
	 * @param status
	 * @param dataArea
	 */
	public static void replyDtu_E0_NOSAVE(Channel channel,Packet packet) {
		try {
			byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(packet.toString()));
			ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
			channel.write(buffer);
//			FileConser.save(packet.getDeviceNo(),packet.toString(), "reply");
			//System.out.println("server -> dtu : " + packet.toString());
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	/**
	 * 回复
	 * @param packet
	 * @param status
	 * @param dataArea
	 * @param channel
	 */
	public static void replyCtrl(Channel channel,Packet packet){
		try {
			byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(packet.toString()));
			ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
			channel.write(buffer);
			FileConser.save(packet.getDeviceNo(),packet.toString(), "reply");
			//System.out.println("server -> ctrl : " + packet.toString());
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	/**
	 * 语音下发
	 * @param gpsNo
	 * @param command
	 */
	public static void voiceReminding(String gpsNo, String command) {
		try {
			// 根据GpsNo查找通信管道
			Channel channel = CacheManager.instance().activeChannel.get(
					CacheManager.instance().sessionMap.get(gpsNo));
			if (null == channel) {
				return;
			}
			byte[] bytes = Conversion.dataEncode(Conversion.hexStringToBytes(command));
			ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes);
			channel.write(buffer);
			FileConser.save(gpsNo, command, "voice");
		} catch (Exception e) {
			Logger.err(e);
		}
	}

}
